package flc.ast.fragment;

import android.graphics.Color;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

import flc.ast.AppConfig;
import flc.ast.R;
import flc.ast.databinding.FragmentRankBinding;
import stark.common.api.StkResApi;
import stark.common.base.IReqRetCallback;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.view.StkTextView;
import stark.common.bean.StkTagResBean;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link RankFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class RankFragment extends BaseNoModelFragment<FragmentRankBinding> {

    private List<StkTagResBean> listType = new ArrayList<>();
    private List<Fragment> fragments = new ArrayList<>();

    @Override
    protected int onCreate() {
        return R.layout.fragment_rank;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(getActivity(), mDataBinding.event1Container);
    }

    @Override
    protected void initData() {
        StkResApi.getChildTagAndResource(null, AppConfig.TYPE_DATA, StkResApi.createParamMap(0, 10), false, new IReqRetCallback<List<StkTagResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkTagResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        listType.addAll(result);
                        initTab();
                    }
                }
            }
        });
    }

    //初始化tab
    private void initTab() {
        for (int i = 0; i < listType.size(); i++) {
            fragments.add(new TabFragment().newInstance(listType.get(i).getHashid()));
            mDataBinding.tlType.addTab(mDataBinding.tlType.newTab().setText(listType.get(i).getName()));
        }
        mDataBinding.vpRank.setAdapter(new MovieAdapter(getChildFragmentManager()));
        mDataBinding.tlType.setupWithViewPager(mDataBinding.vpRank);
        //给tab每个item设置值
        for (int i = 0; i < mDataBinding.tlType.getTabCount(); i++) {
            TabLayout.Tab tab = mDataBinding.tlType.getTabAt(i);
            if (tab != null) {
                tab.setCustomView(getTabView(i));
            }
        }
        // 设置默认第一个被选中
        View view = mDataBinding.tlType.getTabAt(0).getCustomView();
        StkTextView text = view.findViewById(R.id.tvTabItemName);
        text.setBackgroundColor(Color.parseColor("#027EFA"));
        text.setTextColor(Color.parseColor("#FFFFFF"));
        //设置TabLayout点击事件
        mDataBinding.tlType.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                View view = tab.getCustomView();
                StkTextView text = view.findViewById(R.id.tvTabItemName);
                text.setBackgroundColor(Color.parseColor("#027EFA"));
                text.setTextColor(Color.parseColor("#FFFFFF"));
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                View view = tab.getCustomView();
                StkTextView text = view.findViewById(R.id.tvTabItemName);
                text.setBackgroundColor(Color.parseColor("#242F56"));
                text.setTextColor(Color.parseColor("#727E96"));
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    private class MovieAdapter extends FragmentStatePagerAdapter {
        public MovieAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }

        //此方法用来显示tab上的名字
        @Override
        public CharSequence getPageTitle(int position) {
            return listType.get(position).getName();
        }
    }

    //自定义Tab布局
    private View getTabView(int i) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_tab, null);
        StkTextView text = view.findViewById(R.id.tvTabItemName);
        text.setText(listType.get(i).getName());
        text.setBackgroundColor(Color.parseColor("#242F56"));
        text.setTextColor(Color.parseColor("#727E96"));
        return view;
    }

}